home *** CD-ROM | disk | FTP | other *** search
/ ASME's Mechanical Engine…ing Toolkit 1997 December / ASME's Mechanical Engineering Toolkit 1997 December.iso / c_lang / pcc_code.lzh / CHMOD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-02-20  |  4.2 KB  |  179 lines

  1. /* Copyright (c) 1985 Martin Nohr and Tom Serface
  2.  * All Rights Reserved
  3.  *
  4.  * Revision    Date        Description
  5.  * --------    ---------    --------------------------------------------
  6.  *
  7.  * Changes permission attributes on DOS files.  Works somewhat like the 
  8.  * Unix version.  You can use the HEX equivalent or enter =+- attribute
  9.  * to change.
  10.  *        Valid Attributes Are:
  11.  *         00 - Normal File
  12.  *           01 - Read Only File
  13.  *           02 - Hidden File
  14.  *           04 - System File
  15.  *           08 - Volume Label
  16.  *           10 - SubDirectory
  17.  *           20 - Archive File
  18.  *        You can mix any of these for example:
  19.  *           chmod 07 filespec1 filespec2 ... <CR>
  20.  *        Changes all of the files in filespec1 and filespec 2
  21.  *        to Read Only Hidden System Files 01+02+04.
  22.  *        The file attributes are in Hexidecimal.
  23.  *
  24.  *        You can also modify attributes on a file by entering:
  25.  *        '=', '+' or '-' 'n'ormal, 'h'idden, 's'ystem,
  26.  *                        'r'ead only, 'w'riteable, 'a'rchive
  27.  *                        ('w' is the opposite of 'r')
  28.  *          e.g., chmod +rhs filespec1 filespec2 ... <CR>
  29.  *
  30.  */
  31. #include <stdio.h>
  32. #include <fileFind.h>
  33.  
  34. #define PLUS '+'
  35. #define MINUS '-'
  36. #define EQUAL '='
  37. #define EOS '\0'
  38. #define YES 1
  39. #define NO 0
  40.  
  41. main(argc,argv) 
  42. int argc;
  43. char *argv[];
  44. {
  45.     char fspec[85],directory[85],outFile[128],*where;
  46.     char *rindex(), *opts;
  47.     struct DataArea DTA;
  48.     int mods=0, changing=NO;
  49.  
  50.     opts=argv[1];
  51.     if(*opts == PLUS || *opts == MINUS)
  52.         changing=YES;
  53.     else if(*opts == EQUAL) {
  54.         mods=F_NORMAL;
  55.         mods=Set(mods,opts);
  56.     }
  57.     else {
  58.         while(*opts)
  59.             if(!isdigit(*opts++)) 
  60.                 usage();
  61.         sscanf(argv[1],"%x",&mods);
  62.     }
  63.     argc -= 2;
  64.     argv += 2;
  65.     while(argc--) {
  66.         strcpy(fspec, *argv++);
  67.  
  68. /* Set up the Data Transfer Address Structure */
  69.         SetDta(&DTA);
  70.         strcpy(directory,fspec);
  71.         if((where=rindex(directory,'\\')) || (where=rindex(directory,':'))) {
  72.             *(where+1)=EOS;
  73.         }
  74.         else {
  75.             directory[0]=EOS;
  76.         }
  77.         if (findFirst(fspec,F_ALL)) {
  78.             do {
  79.                 strcpy(outFile,directory);
  80.                 strcat(outFile,DTA.dta_name);
  81.                 if(changing) {
  82.                     GetMod(outFile,&mods);
  83.                     mods=Set(mods,opts);
  84.                 }
  85.                 if(ChMod(outFile,mods) != 0)
  86.                     printf("Change failed for %s\n",outFile);
  87.             } while (findNext(F_ALL));
  88.         }
  89.         else
  90.             printf("Changed failed for %s\n",fspec);
  91.     }
  92. }
  93.  
  94. usage()
  95. {
  96.         printf("Valid Attributes Are:\n");
  97.         printf("   00 - Normal File\n");
  98.         printf("   01 - Read Only File\n");
  99.         printf("   02 - Hidden File\n");
  100.         printf("   04 - System File\n");
  101.         printf("   08 - Volume Label\n");
  102.         printf("   10 - SubDirectory\n");
  103.         printf("   20 - Archive File\n");
  104.         printf("You can mix any of these for example:\n");
  105.         printf("   chmod 07 filespec1 filespec2 ... <CR>\n");
  106.         printf("Changes all of the files in filespec1 and filespec 2\n");
  107.         printf("to Read Only Hidden System Files 01+02+04.\n");
  108.         printf("The file attributes are in Hexidecimal.\n");
  109.         printf("\nYou can also modify attributes on a file by entering:\n");
  110.         printf("'=', '+' or '-' 'n'ormal, 'h'idden, 's'ystem,\n");
  111.         printf("                'r'ead only, 'w'riteable, 'a'rchive\n");
  112.         printf("                ('w' is the opposite of 'r')\n");
  113.         printf("  e.g., chmod +rhs filespec1 filespec2 ... <CR>\n");
  114.         exit(1);
  115. }            
  116.  
  117. Set(mods,opts)
  118. int mods;
  119. char *opts;
  120. {
  121.         int add;
  122.  
  123.         add=(*opts==PLUS || *opts==EQUAL);
  124.         ++opts;
  125.         while(*opts) {
  126.             switch(tolower(*opts)) {
  127.                 case 'n':
  128.                     mods=0;
  129.                     break;
  130.                 case 'r':
  131.                     if(add)
  132.                         mods |= F_READ_ONLY;
  133.                     else    
  134.                         mods &= ~F_READ_ONLY;
  135.                     break;
  136.                 case 'w':
  137.                     if(add)
  138.                         mods &= ~F_READ_ONLY;
  139.                     else    
  140.                         mods |= F_READ_ONLY;
  141.                     break;
  142.                 case 'h':
  143.                     if(add)
  144.                         mods |= F_HIDDEN;
  145.                     else
  146.                         mods &= ~F_HIDDEN;
  147.                     break;
  148.                 case 's':                
  149.                     if(add)
  150.                         mods |= F_SYSTEM;
  151.                     else
  152.                         mods &= ~F_SYSTEM;
  153.                     break;
  154.                 case 'a':            
  155.                     if(add)
  156.                         mods |= F_ARCHIVE;
  157.                     else
  158.                         mods &= ~F_ARCHIVE;
  159.                     break;
  160.                 case 'd':            
  161.                     if(add)
  162.                         mods |= F_DIRECTORY;
  163.                     else
  164.                         mods &= ~F_DIRECTORY;
  165.                     break;
  166.                 case 'l':            
  167.                     if(add)
  168.                         mods |= F_LABEL;
  169.                     else
  170.                         mods &= ~F_LABEL;
  171.                     break;
  172.                 default:
  173.                     usage();
  174.             }
  175.             ++opts;
  176.         }
  177.     return(mods);
  178. }
  179.